package Stack;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-04
 * Time:15:20
 * Text：844. 比较含退格的字符串
 */
public class Text2 {
    public static boolean backspaceCompare(String s, String t) {
        char[] arrs=s.toCharArray();
        char[] arrt=t.toCharArray();
        int j1=0;
        for(int i=0;i<arrs.length;i++){
            if(s.charAt(i)=='#'){
                if(j1==0)continue;
                j1-=1;
                continue;
            }
            arrs[j1++]=s.charAt(i);
        }
        int j2=0;
        for(int i=0;i<arrt.length;i++){
            if(t.charAt(i)=='#'){
                if(j2==0)continue;
                j2-=1;
                continue;
            }
            arrt[j2++]=t.charAt(i);
        }
        return String.valueOf(arrs,0,j1).equals(String.valueOf(arrt,0,j2));
    }
    public static String backspaceCompare2(String s) {
        StringBuffer str=new StringBuffer();
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);
            if(ch!='#') {
                str.append(ch);
            }else{
                if(str.length()>0) str.deleteCharAt(str.length()-1);
            }
        }
        return str.toString();
    }
    public static void main(String[] args) {
        System.out.println(backspaceCompare2("#").equals(backspaceCompare2("ab#")));
        System.out.println(backspaceCompare("#","ab#"));
    }
}
